package com.action;

import com.dao.UserInfoDao;
import com.entity.Userinfo;
import com.opensymphony.xwork2.ActionSupport;

import java.util.ArrayList;
import java.util.List;

public class UserAction extends ActionSupport {

    private String username;
    private String password;
    private Userinfo userinfo;
    private List<Userinfo> userinfoList = new ArrayList<>();
    private UserInfoDao userInfoDao = new UserInfoDao();

    public String addUser() {
        userInfoDao.save(userinfo);
        return SUCCESS;
    }

    public String login() {
        String sql = "select * from userinfo where username = '" + username + "' and `password` = '" + password + "'";
        List<Userinfo> userinfoList = userInfoDao.searchList(sql);
        if (userinfoList != null && userinfoList.size() > 0) {
            return SUCCESS;
        } else {
            return INPUT;
        }
    }

    public String listUsers() {
        String sql = "select * from userinfo";
        userinfoList = userInfoDao.searchList(sql);
        return SUCCESS;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Userinfo getUserinfo() {
        return userinfo;
    }

    public void setUserinfo(Userinfo userinfo) {
        this.userinfo = userinfo;
    }

    public List<Userinfo> getUserinfoList() {
        return userinfoList;
    }

    public void setUserinfoList(List<Userinfo> userinfoList) {
        this.userinfoList = userinfoList;
    }
}
